package cn.jiadakeji.parent.controller;

import cn.jiadakeji.parent.common.core.controller.BaseController;
import cn.jiadakeji.parent.common.core.domain.AjaxResult;
import cn.jiadakeji.parent.common.util.SecurityUtils;
import cn.jiadakeji.parent.domain.Customer;
import cn.jiadakeji.parent.domain.CustomerParentPart;
import cn.jiadakeji.parent.domain.Student;
import cn.jiadakeji.parent.domain.StudentParent;
import cn.jiadakeji.parent.dto.CustomerJwtDTO;
import cn.jiadakeji.parent.dto.CustomerParentPartDTO;
import cn.jiadakeji.parent.mapper.CustomerMapper;
import cn.jiadakeji.parent.mapper.CustomerParentPartMapper;
import cn.jiadakeji.parent.mapper.StudentParentMapper;
import cn.jiadakeji.parent.service.StudentParentRelationService;
import cn.jiadakeji.parent.vo.CustomerParentPartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "用户信息", tags = {"用户信息接口"})
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {
    @Resource
    private CustomerParentPartMapper customerParentPartMapper;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private StudentParentMapper studentParentMapper;


    @ApiOperation("获取用户详情")
    @GetMapping("/get")
    public AjaxResult getCustomerInfo() {
        CustomerJwtDTO loginUser = SecurityUtils.getLoginUser();
        Customer customer = new Customer();
        customer.setId(loginUser.getId());
        CustomerParentPartDTO customerParentPartDTO = customerParentPartMapper.getCustomerParentPart(customer);
        return AjaxResult.success(customerParentPartDTO);
    }

    @ApiOperation("更新用户基本信息")
    @PostMapping("/update")
    public AjaxResult getCustomerInfo(@RequestBody @Validated CustomerParentPartVO customerParentPartVO) {
        CustomerJwtDTO loginUser = SecurityUtils.getLoginUser();
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerParentPartVO.getCustomerVO(), customer);
        customer.setId(loginUser.getId());

        StudentParent studentParent = new StudentParent();
        CustomerParentPart customerParentPart = customerParentPartMapper.selectOneByCustomerId(customer.getId());
        studentParent.setId(customerParentPart.getStudentParentId());
        studentParent.setPhone(customer.getPhone());
        studentParent.setParentName(customer.getName());
        studentParentMapper.updateById(studentParent);

        return toAjax(customerMapper.updateById(customer));
    }


}
